import 'package:flutter/material.dart';
import 'utils.dart';
import 'base_layout.dart';

///
/// 表格部局
///
enum TableLayoutType { table, pagedTable }

class TableLayout extends BaseLayout {
  //表格类型，table普通表格，pagedTable分页表格
  final TableLayoutType tableType;
  //标题，只用于pagedtable
  final Widget header;
  //
  final List<Widget> actions;
  //表头列
  final List<DataColumn> columns;
//排序列索引
  final int sortColumnIndex;
//是否升序
  final bool sortAscending;
//全选事件
  final ValueSetter<bool> onSelectAll;
//初始索引 只用于pagedtable
  final int initialFirstRowIndex;
//页数更改监听
  final ValueChanged<int> onPageChanged;
//默认一页显示的行数 只用于pagedtable
  final int rowsPerPage;
//
  static const int defaultRowsPerPage = 10;
//可选择页数 只用于pagedtable
  final List<int> availableRowsPerPage;
//点击可选择页数下拉监听 只用于pagedtable
  final ValueChanged<int> onRowsPerPageChanged;
//数据源，只用于pagedtable
  final DataTableSource source;
//内容行，只用于table
  final List<DataRow> rows;

  TableLayout({
    Key key,
    AlignmentGeometry alignment,
    double width,
    double height,
    bool isVisible,
    Decoration decoration,
    EdgeInsetsGeometry padding,
    EdgeInsetsGeometry margin,
    this.tableType,
    this.header,
    this.actions,
    @required this.columns,
    this.sortColumnIndex,
    this.sortAscending = true,
    this.onSelectAll,
    this.initialFirstRowIndex = 0,
    this.onPageChanged,
    this.rowsPerPage = defaultRowsPerPage,
    this.availableRowsPerPage = const <int>[
      defaultRowsPerPage,
      defaultRowsPerPage * 2,
      defaultRowsPerPage * 5,
      defaultRowsPerPage * 10
    ],
    this.onRowsPerPageChanged,
    this.rows,
    this.source,
  }) : super(
            key: key,
            alignment: alignment,
            width: width,
            height: height,
            isVisible: isVisible,
            padding: padding,
            margin: margin,
            decoration: decoration);
  //普通表格构造
  TableLayout.table({
    Key key,
    AlignmentGeometry alignment,
    double width,
    double height,
    bool isVisible,
    Decoration decoration,
    EdgeInsetsGeometry padding,
    EdgeInsetsGeometry margin,
    List<DataColumn> columns,
    List<DataRow> rows,
    int sortColumnIndex,
    bool sortAscending,
    ValueSetter<bool> onSelectAll,
  }) : this(
            key:key,
            tableType: TableLayoutType.table,
            rows: rows,
            columns:columns,
            sortAscending: sortAscending,
            sortColumnIndex: sortColumnIndex,
            onSelectAll: onSelectAll);
  //分页表格构造
  TableLayout.pagedTable({
    Key key,
    AlignmentGeometry alignment,
    double width,
    double height,
    bool isVisible,
    Decoration decoration,
    EdgeInsetsGeometry padding,
    EdgeInsetsGeometry margin,
    @required List<DataColumn> columns,
    int sortColumnIndex,
    bool sortAscending,
    ValueSetter<bool> onSelectAll,
    @required Widget header,
    List<Widget> actions,
    int initialFirstRowIndex,
    ValueChanged<int> onPageChanged,
    int rowsPerPage,
    List<int> availableRowsPerPage,
    ValueChanged<int> onRowsPerPageChanged,
    DataTableSource source,
  }) : this(
          key:key,
          tableType: TableLayoutType.pagedTable,
          sortAscending: sortAscending,
          sortColumnIndex: sortColumnIndex,
          onSelectAll: onSelectAll,
          columns:columns,
          header: header,
          actions: actions,
          initialFirstRowIndex: initialFirstRowIndex,
          onPageChanged: onPageChanged,
          rowsPerPage: rowsPerPage,
          availableRowsPerPage: availableRowsPerPage,
          onRowsPerPageChanged: onRowsPerPageChanged,
          source: source,
        );
  @override
  _TableLayoutState createState() => _TableLayoutState();
}

class _TableLayoutState extends State<TableLayout> with BaseState {
  @override
  void initState() {
    baseLayout = widget;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return buildState(buildContent);
  }

  @override
  Widget buildContent() {
    return widget.tableType == TableLayoutType.pagedTable
        ? _createPagedTable()
        : _createTable();
  }

  _createTable() {
    return DataTable(
      columns: widget.columns,
      rows: widget.rows,
      sortColumnIndex: widget.sortColumnIndex,
      sortAscending: widget.sortAscending,
      onSelectAll: widget.onSelectAll,
    );
  }
  _createPagedTable() {
    return PaginatedDataTable(
      columns: widget.columns,
      sortColumnIndex: widget.sortColumnIndex,
      sortAscending: widget.sortAscending,
      onSelectAll: widget.onSelectAll,
      header: widget.header,
      actions: widget.actions,
      initialFirstRowIndex: widget.initialFirstRowIndex,
      onPageChanged: widget.onPageChanged,
      rowsPerPage: widget.rowsPerPage,
      availableRowsPerPage: widget.availableRowsPerPage,
      onRowsPerPageChanged: widget.onRowsPerPageChanged,
      source: widget.source,
    );
  }
}

class WDataColumn{
  final Key key;
  final String label;
  final String tooltip;
  final bool numeric;
  final bool sorted;
  WDataColumn({
    this.key,
    this.label,
    this.tooltip,
    this.numeric,
    this.sorted
});
}
class WDataRow{
  final Key key;
  final bool selected;
  final ValueChanged<bool> onSelectChanged;
  List<DataCell> cells;
  WDataRow({this.key,this.selected,this.onSelectChanged,this.cells});
}

class WDataTableSource<WDataRow> extends DataTableSource{
  final List<dynamic> rows;
  WDataTableSource({this.rows}):assert(rows!=null);
  int _selectCount=0;
  @override
  DataRow getRow(int index) {
    return DataRow.byIndex(
        index: index,
        cells: rows[0].cells,
    );
  }

  @override
  int get selectedRowCount=>_selectCount;

  @override
  bool get isRowCountApproximate =>false;

  @override
  int get rowCount {
    return rows.length;
  }
  _sort(int index,bool ascending){
      rows.sort((a,b){
      if (!ascending) {
        final c = a;
        a = b;
        b = c;
      }
      return a.cells[index].compareTo(b.cells[index]);
    });
    notifyListeners();
  }
}